# from icecream import ic
import json
import os
import sys
import pandas as pd
import requests

from get_sql_res import gen_run_detail, gen_data_cluster
from get_response import gen_wafermap_config_mapping,gen_wafermap_config
from gen_info_dic import gen_info_dic

directory_path = os.path.dirname(os.path.abspath(__file__))
file_path=os.path.join(directory_path,'cfgs/mapping_char.json')
with open(file_path, 'r',encoding='utf8') as file:
    rule_detail_maping = json.load(file)

def gen_cover_page(info_dic,df_run_detail_raw):
    df_lst=[]
    input_dict=info_dic['input_dict']
    output_dict=info_dic['output_dict']
    for id in input_dict:
        a1 = f"{input_dict[id]}:{output_dict[id]}"
        a2 = f"Data Scope:{info_dic['device_id']}, {info_dic['test_stage']}"
        a3 = f"Step Overview:"
        rule_alias = list(df_run_detail_raw[df_run_detail_raw['updated_input_virtual_id']==id]['rule_alias'].drop_duplicates())
        for rule_alia in rule_alias:
            a3 += f'{rule_alia} -> '
        a3 = a3[:-4]
        df = pd.DataFrame({'data': [a1, a2, a3]})
        df_lst.append(df)
    return df_lst

if __name__ == '__main__':
    # from icecream import ic

    with open('./input/aaa.json', 'r') as file:
        config = json.load(file)

    rule_run_record_id = config['recordId']
    sql_info = {}
    sql_info['mysqlInfo'] = config['mysqlInfo']
    sql_info['ckInfo'] = config['ckInfo']
    sql_info['dasHost'] = config['dasHost']
    re_file_path = config['fileFullPath']

    df_run_detail = gen_run_detail(rule_run_record_id, sql_info)


    info_dic=gen_info_dic(df_run_detail, sql_info)

    ##
    df_lst=gen_cover_page(info_dic, df_run_detail)
    # ic(df_lst)